'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 副本战斗记录
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');
var _ = require('lodash');
var autoIncrement = require('mongoose-auto-increment');
var luckItemSchema = require('./luck/luckItemSchema');

/**
 * 副本战斗记录
 */
var copyRecordSchema = new mongoose.Schema({
    // 进攻方角色id
    atk_role_id: {
        type: Number,
        required: true
    },

    // 副本id
    copy_id: {
        type: Number,
        required: true
    },

    // 攻击方数据
    atk: {
        hero: {},
        equip: {
            weapon: {},
            head: {},
            shoulder: {},
            armor: {},
            leg: {},
            necklace: {},
            ring: {},
            ear_ring: {}
        },
        soldier_list: [],
        skill_list: []
    },

    // 防守方数据
    def: {
        // 名字
        name: {
            type: String,
            default: ''
        },

        // 总共的金币
        total_gold: {
            type: Number
        },

        // 总共的经验
        total_exp: {
            type: Number
        },

        // 建筑列表
        build_list: [],

        // 道路列表
        road_list: [Number],

        // 士兵列表
        soldier_list: []
    },

    // 星级
    star: {
        type: Number
    },

    // 进度
    progress: {
        type: Number,
        default: 0
    },

    // 是否已经领取奖励
    is_reward: {
        type: Number,
        default: 0
    },

    // 创建时间
    create_time: {
        type: Date,
        default: Date.now
    }
});

/**
 * 获取战斗记录的编号
 */
copyRecordSchema.methods.getRecordId = function () {
    return this._id;
};

/**
 * 获取进攻方的角色id
 * @return {[type]} [description]
 */
copyRecordSchema.methods.getAtkRoleId = function () {
    return this.atk_role_id;
};

/**
 * 获取奖杯数量
 * @return {[type]} [description]
 */
copyRecordSchema.methods.getCup = function () {
    return 0;
};

/**
 * 防守方名
 * @return {[type]} [description]
 */
copyRecordSchema.methods.getDefName = function () {
    return this.def.name;
};

/**
 * 获取防守方的建筑列表
 */
copyRecordSchema.methods.getDefBuildList = function () {
    return this.def.build_list;
};

/**
 * 获取防守方的金币总数
 */
copyRecordSchema.methods.getDefTotalGold = function () {
    return 0;
};

/**
 * 获取防守方的经验
 */
copyRecordSchema.methods.getDefTotalExp = function () {
    return this.def.total_exp;
};

/**
 * 获取星级
 * @param {[type]} star [description]
 */
copyRecordSchema.methods.getStar = function () {
    return this.star;
};

/**
 * 设置星级
 * @param {[type]} star [description]
 */
copyRecordSchema.methods.setStar = function (star) {
    this.star = +star;
};

/**
 * 获取进度
 * @return {[type]} [description]
 */
copyRecordSchema.methods.setProgress = function (progress) {
    this.progress = progress;
};

/**
 * 获取进度
 * @return {[type]} [description]
 */
copyRecordSchema.methods.getProgress = function () {
    return this.progress;
};

/**
 * 设置领取了奖励
 * @return {[type]} [description]
 */
copyRecordSchema.methods.setReward = function () {
    this.is_reward = 1;
};

/**
 * 是否三个宝箱都被开启
 */
copyRecordSchema.methods.canReward = function () {
    // 如果星级小于1星,不能抽奖
    if (this.star < 1 || this.star === undefined) {
        return false;
    }

    if (!!this.is_reward) {
        return false;
    }

    return true;
};

copyRecordSchema.plugin(autoIncrement.plugin, {
    model: 'CopyRecordSchema',
    startAt: 10000
});

if (!copyRecordSchema.options.toJSON) {
    copyRecordSchema.options.toJSON = {};
}
/* jshint unused:false */
copyRecordSchema.options.toJSON.transform = function (doc, ret) {
    ret.create_time = moment(ret.create_time).unix();
    delete ret._id;
    delete ret.__v;
};

mongoose.model('CopyRecord', copyRecordSchema);
